package com.jvm123.minio.service.client;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import com.google.common.collect.Multimap;

import io.minio.AbortMultipartUploadResponse;
import io.minio.CreateMultipartUploadResponse;
import io.minio.ListPartsResponse;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.UploadPartResponse;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.minio.messages.Part;

/**
 * 将MinioClient的getUploadId、uploadPart、completeMultipartUpload、abortMultipartUpload、listParts方法改为public
 * @author freeman
 *
 */
public class ExtendMinioClient extends MinioClient {

	protected ExtendMinioClient(MinioClient client) {
		super(client);
	}

	/**
	 * 获取uploadId
	 * 
	 * @param bucketName
	 * @param region
	 * @param objectName
	 * @param headers
	 * @param extraQueryParams
	 * @return
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws InsufficientDataException
	 * @throws ServerException
	 * @throws XmlParserException
	 * @throws ErrorResponseException
	 * @throws InternalException
	 * @throws InvalidResponseException
	 * @throws IOException
	 */
	public String getUploadId(String bucketName, String region, String objectName, Multimap<String, String> headers,
			Multimap<String, String> extraQueryParams)
			throws InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, ServerException,
			XmlParserException, ErrorResponseException, InternalException, InvalidResponseException, IOException {
		CreateMultipartUploadResponse response = createMultipartUpload(bucketName, region, objectName, headers,
				extraQueryParams);
		return response.result().uploadId();
	}

	/**
	 * 上传分片数据
	 */
	public UploadPartResponse uploadPart(String bucketName, String region, String objectName, Object data, int length,
			String uploadId, int partNumber, Multimap<String, String> extraHeaders,
			Multimap<String, String> extraQueryParams)
			throws NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException,
			ServerException, XmlParserException, ErrorResponseException, InternalException, InvalidResponseException {
		return super.uploadPart(bucketName, region, objectName, data, length, uploadId, partNumber, extraHeaders,
				extraQueryParams);
	}

	/**
	 * 完成分片上传
	 */
	public ObjectWriteResponse completeMultipartUpload(String bucketName, String region, String objectName,
			String uploadId, Part[] parts, Multimap<String, String> extraHeaders,
			Multimap<String, String> extraQueryParams)
			throws NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException,
			ServerException, XmlParserException, ErrorResponseException, InternalException, InvalidResponseException {
		return super.completeMultipartUpload(bucketName, region, objectName, uploadId, parts, extraHeaders,
				extraQueryParams);
	}

	/**
	 * 取消分片上传
	 */
	public AbortMultipartUploadResponse abortMultipartUpload(String bucketName, String region, String objectName,
			String uploadId, Multimap<String, String> extraHeaders, Multimap<String, String> extraQueryParams)
			throws NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException,
			ServerException, XmlParserException, ErrorResponseException, InternalException, InvalidResponseException {
		return super.abortMultipartUpload(bucketName, region, objectName, uploadId, extraHeaders, extraQueryParams);
	}

	public ListPartsResponse listParts(String bucketName, String region, String objectName, Integer maxParts,
			Integer partNumberMarker, String uploadId, Multimap<String, String> extraHeaders,
			Multimap<String, String> extraQueryParams)
			throws NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException,
			ServerException, XmlParserException, ErrorResponseException, InternalException, InvalidResponseException {
		return super.listParts(bucketName, region, objectName, maxParts, partNumberMarker, uploadId, extraHeaders,
				extraQueryParams);
	}
}
